package com.xuxin.pda.model;

import com.xuxin.pda.bean.CompanyBean;
import com.xuxin.pda.bean.TrashExitBean;
import com.xuxin.pda.bean.WarehouseBean;
import com.xuxin.pda.contract.TrashExitContract;
import com.xuxin.pda.network.Retrofit;
import com.xuxin.pda.network.RetrofitApi;
import com.xuxin.pda.presenter.TrashExitPresenter;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.observers.DisposableObserver;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by xu_xin
 * Created on 2021/2/7 0007
 */
public class TrashExitModel implements TrashExitContract.Model {
    @Override
    public void GetWarehouseInfo(TrashExitPresenter presenter, String WarehouseId) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .GetWarehouseInfo(WarehouseId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<WarehouseBean>() {
                    @Override
                    public void onNext(@NonNull WarehouseBean warehouseBean) {
                        if (warehouseBean.isSuccess()) {
                            presenter.onSuccess(warehouseBean);
                        } else {
                            presenter.onError(warehouseBean);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }

    @Override
    public void trashExit(TrashExitPresenter presenter, String name, String WarehouseId, String UserId, String message) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .trashExit(name, WarehouseId, UserId, message)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<TrashExitBean>() {
                    @Override
                    public void onNext(@NonNull TrashExitBean trashExitBean) {
                        if (trashExitBean.isSuccess()) {
                            presenter.onSuccess(trashExitBean);
                        } else {
                            presenter.onError(trashExitBean);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }

    @Override
    public void GetCompany(TrashExitPresenter presenter) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .getCompany(0,20)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<CompanyBean>() {
                    @Override
                    public void onNext(@NonNull CompanyBean companyBean) {
                        if (companyBean.isSuccess()) {
                            presenter.onSuccess(companyBean);
                        } else {
                            presenter.onError(companyBean);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }
}